package com.taiyi.renaissancemall.ui.mine.team;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.MyTeamCountBean;
import com.taiyi.renaissancemall.bean.MyTeamListBean;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//团队页面
public class MyTeamActivity extends BaseActivity<MyTeamView, MyTeamPresent> implements MyTeamView, SpringView.OnFreshListener {

    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.yellow)
    TextView yellow;
    @BindView(R.id.dateTv)
    TextView dateTv;
    @BindView(R.id.teamStatistics)
    TextView teamStatistics;
    @BindView(R.id.teamSizeTv)
    TextView teamSizeTv;
    @BindView(R.id.teamAddTv)
    TextView teamAddTv;
    @BindView(R.id.pushStatistics)
    TextView pushStatistics;
    @BindView(R.id.pushSizeTv)
    TextView pushSizeTv;
    @BindView(R.id.pushAddTv)
    TextView pushAddTv;
    @BindView(R.id.topB)
    TextView topB;
    @BindView(R.id.myHardcore)
    TextView myHardcore;
    @BindView(R.id.myFriend)
    TextView myFriend;
    @BindView(R.id.myTeam)
    TextView myTeam;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.noDataIv)
    ImageView noDataIv;
    private int page = 1;
    private String title = "1";
    private int mYear;
    private int mMonth;
    private int mDay;
    private String dateText = "";
    private MyTeamAdapter myTeamAdapter;
    private List<MyTeamListBean.TuanlistBean> data = new ArrayList<>();
    private List<MyTeamListBean.TuanlistBean> comlist;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_team;
    }

    @Override
    public MyTeamPresent initPresenter() {
        return new MyTeamPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        titleTv.setText(getString(R.string.myTeam));
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        setAdapter(data);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).init();
    }

    @Override
    protected void initData() {

        //获取并显示当前日期
        Calendar ca = Calendar.getInstance();
        mYear = ca.get(Calendar.YEAR);
        mMonth = ca.get(Calendar.MONTH);
        mDay = ca.get(Calendar.DAY_OF_MONTH);
        dateText = mYear + "-" + (mMonth + 1) + "-" + mDay;
        dateTv.setText(dateText);

        showProgress();
        //请求团队数据和列表数据
        presenter.getMyTeamCount(dateTv.getText().toString().trim());
        presenter.getMyTeamList(title, page + "");
        myHardcore.setSelected(true);
        myFriend.setSelected(false);
        myTeam.setSelected(false);

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void MyTeamSuccess(MyTeamCountBean myTeamCountBean) {
        hideProgress();
        teamSizeTv.setText("团队人数：" + myTeamCountBean.tuanz);
        teamAddTv.setText("当日新增：" + myTeamCountBean.tuand);
        pushSizeTv.setText("直服人数：" + myTeamCountBean.fatherz);
        pushAddTv.setText("当日新增：" + myTeamCountBean.fatherd);
    }

    @Override
    public void MyTeamFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void MyTeamListSuccess(MyTeamListBean myTeamListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        comlist = myTeamListBean.tuanlist;
        if (page == 1) {
            data.clear();
            if (comlist == null || comlist.size() == 0) {
                noDataIv.setVisibility(View.VISIBLE);
            } else {
                noDataIv.setVisibility(View.GONE);
            }
        }
        data.addAll(comlist);
        if (myTeamAdapter != null) {
            myTeamAdapter.notifyDataSetChanged();
        }

    }

    private void setAdapter(List<MyTeamListBean.TuanlistBean> tuanlistBeanList) {
        myTeamAdapter = new MyTeamAdapter(R.layout.mine_my_team_item_layout, tuanlistBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(MyTeamActivity.this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(myTeamAdapter);
    }

    @Override
    public void MyTeamListFailure(String msg) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        ToastUtils.showLongToast(msg);
    }

    @OnClick({R.id.dateTv, R.id.myHardcore, R.id.myFriend, R.id.myTeam, R.id.backIv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.dateTv:
                DatePickerDialog datePickerDialog = new DatePickerDialog(MyTeamActivity.this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                                mYear = year;
                                mMonth = month;
                                mDay = dayOfMonth;
                                dateText = year + "-" + (month + 1) + "-" + dayOfMonth;
                                dateTv.setText(dateText);

                                presenter.getMyTeamCount(dateTv.getText().toString().trim());
                                if (myHardcore.isSelected()) {
                                    page = 1;
                                    presenter.getMyTeamList("1", page + "");
                                } else if (myFriend.isSelected()) {
                                    page = 1;
                                    presenter.getMyTeamList("2", page + "");
                                } else if (myTeam.isSelected()) {
                                    page = 1;
                                    presenter.getMyTeamList("3", page + "");
                                }

                            }
                        },
                        mYear, mMonth, mDay);
                datePickerDialog.show();
                break;
            case R.id.myHardcore:
                myHardcore.setSelected(true);
                myFriend.setSelected(false);
                myTeam.setSelected(false);
                title = "1";
                showProgress();
                presenter.getMyTeamList(title, page + "");
                break;
            case R.id.myFriend:
                myHardcore.setSelected(false);
                myFriend.setSelected(true);
                myTeam.setSelected(false);
                title = "2";
                showProgress();
                presenter.getMyTeamList(title, page + "");
                break;
            case R.id.myTeam:
                myHardcore.setSelected(false);
                myFriend.setSelected(false);
                myTeam.setSelected(true);
                title = "3";
                showProgress();
                presenter.getMyTeamList(title, page + "");
                break;
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        presenter.getMyTeamList(title, page + "");
    }

    @Override
    public void onLoadmore() {
        page++;
        presenter.getMyTeamList(title, page + "");
    }
}
